package com.example.day0525

import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.base.entity.NewsBeanItem
import com.example.base.mvvm.BaseActivity
import com.example.day0525.databinding.ActivityMainBinding
import com.example.day0525.news.NewAdapter
import com.example.day0525.news.NewsAdapter
import com.example.day0525.news.NewsViewModel
import com.example.day0525.news.OnItemClickListener
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@AndroidEntryPoint
class MainActivity : BaseActivity<ActivityMainBinding,NewsViewModel>() {
    val newsAdapter by lazy { NewsAdapter() }
    //lateinit var newAdapter: NewAdapter
    override fun initView() {

        binding.rv.apply {
            adapter=newsAdapter
            layoutManager=LinearLayoutManager(this@MainActivity)
        }
        lifecycleScope.launch {
            vm.getNews().collect(){
                newsAdapter.submitData(it)
            }
        }
        newsAdapter.setOnItemClickListener(object : OnItemClickListener{
            override fun onItemClick(position: Int, item: NewsBeanItem) {

                ARouter.getInstance().build("/app/XqActivity").navigation()
            }
        })
//        newAdapter=NewAdapter()
//        binding.rv.layoutManager=LinearLayoutManager(this)
//        binding.rv.adapter=newsAdapter
//        vm.getNew()

      ItemTouchHelper(object :ItemTouchHelper.Callback(){
            override fun getMovementFlags(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder
            ): Int {
                val left = ItemTouchHelper.LEFT
                return makeMovementFlags(0,left)
            }

            override fun onMove(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder,
                target: RecyclerView.ViewHolder
            ): Boolean {
               return true
            }

            override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
                val position = viewHolder.layoutPosition
                newsAdapter.notifyItemRemoved(position)
            }
        }).attachToRecyclerView(binding.rv)
    }

    override fun initData() {

        vm.success.observe(this){
            ToastUtils.showLong(it.toString())
        }
    }

}